package view.inventory
{
import core.model.PlayerModel;
import core.model.events.PlayerModelEvent;

import flash.display.MovieClip;
import flash.geom.Point;

import framework.view.components.container.IRenderProxy;

import values.inventory.InventoryValue;
import values.item.IItemValue;
import values.item.ItemTemplateValueList;

import view.component.window.ClassifiedPaginationWindowProxy;
import view.component.window.event.NavigationChangeEvent;

/**
 * InventoryUIProxy
 * @author jliang
 * 
 */
public class InventoryUIProxy extends ClassifiedPaginationWindowProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function InventoryUIProxy()
	{
		super();
	}
	//==========================================================================
	//  Variables
	//==========================================================================
	[Inject]
	public var playerModel:PlayerModel;
	//==========================================================================
	//  Overridden methods: ComponentProxy
	//==========================================================================
	override protected function get repeaterItemRenderClass():Class
	{
		return InventoryItemRenderProxy;
	}
	
	override protected function get navItemRenderClass():Class
	{
		return InventoryNavRenderProxy;
	}
	
	override protected function get navData():*
	{
		return playerModel.inventoryList;
	}
	
	override protected function get currentTypeDataLength():*		
	{
		return currentTypeData.length;
	}
	
	override public function parse(target:MovieClip):void
	{
		super.parse(target);
		
		playerModel.addEventListener(PlayerModelEvent.ADD_ITEM, itemChangedHandler);
		playerModel.addEventListener(PlayerModelEvent.REMOVE_ITEM, itemChangedHandler);
	}
	
	override protected function initData():void
	{
		var list:Array = navData;
		for each (var vo:InventoryValue in list)
		{
			addTypeData(vo.key, vo.items);
		}
	}
	
	override public function show(lock:Boolean=true, position:Point=null, ...args):void
	{
		super.show.apply(this, [ lock, position ].concat(args));
	}
	//==========================================================================
	//  Public methods
	//==========================================================================
	public function showInventory(type:String = null):void
	{
		show();
		
		if (type == null && currentType == null) type = "equipment";
		
		if (type)
		{
			for each (var render:IRenderProxy in navRepeater.itemProxys)
			{
				if (render.data && InventoryValue(render.data).key == type)
				{
					render.selected = true;
					break;
				}
			}
		}
	}
	
	public function refresh():void
	{
		var pageCount:int = Math.ceil(currentTypeDataLength / preCount);
		
		var page:int = currentPage;
		if (pageCount - 1 < currentPage)
		{
			page = pageCount - 1;
			if (page < 0)
			{
				page = 0;
			}
		}
		updateList(currentTypeData, currentTypeDataLength, page);
	}
	//==========================================================================
	//  Protected methods
	//==========================================================================
	protected function getItemRenderByData(data:IItemValue):IRenderProxy
	{
		for each (var render:IRenderProxy in itemRepeater.itemProxys)
		{
			if (render.data == data)
			{
				return render;
			}
		}
		return null;
	}
	
	protected function getItemRenderByItemKey(key:String):Vector.<IRenderProxy>
	{
		var temp:Vector.<IRenderProxy> = new Vector.<IRenderProxy>();
		for each (var render:IRenderProxy in itemRepeater.itemProxys)
		{
			if (render.data && IItemValue(render.data).key == key)
			{
				temp.push(render);
			}
		}
		return temp;
	}
	//==========================================================================
	//  Event handlers
	//==========================================================================	
	override protected function navigationChangeHandler(event:NavigationChangeEvent):void
	{
		var vo:InventoryValue = InventoryValue(event.param);
		for each (var render:IRenderProxy in navRepeater.itemProxys)
		{
			if (render.data && InventoryValue(render.data).key != vo.key)
			{
				render.selected = false;
			}
		}
		currentType = vo.key;
		updateList(currentTypeData, currentTypeDataLength, 0);
	}
	
	protected function itemChangedHandler(event:PlayerModelEvent):void
	{
		if (!currentType) return;
		
		var items:* = event.params.items;
		var key:String = event.params.key;
		
		if (items)
		{
			refresh();
		}
		else
		{
			if (!ItemTemplateValueList.getByKey(key).showInInventory) return;
			
			var renders:Vector.<IRenderProxy> = getItemRenderByItemKey(key);
			
			for each (var render:IRenderProxy in renders)
			{
				InventoryItemRenderProxy(render).update();
			}
		}
	}
}
}




